package com.asa.voice.mp3.asa.output;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

import com.asa.voice.mp3.asa.decoder.Header;
import com.asa.voice.mp3.asa.decoder.IAudio;

public class Audio implements IAudio {
	  private SourceDataLine dateline;
	  
	  public boolean open(Header paramHeader, String paramString) {
	    AudioFormat audioFormat = new AudioFormat(paramHeader.getSamplingRate(), 16, paramHeader.getChannels(), true, false);
	    try {
	      this.dateline = AudioSystem.getSourceDataLine(audioFormat);
	      this.dateline.open(audioFormat, 8 * paramHeader.getPcmSize());
	    } catch (LineUnavailableException lineUnavailableException) {
	      System.err.println("初始化音频失败");
	      return false;
	    } 
	    this.dateline.start();
	    return true;
	  }
	  
	  public int write(byte[] paramArrayOfbyte, int paramInt) {
	    return this.dateline.write(paramArrayOfbyte, 0, paramInt);
	  }
	  
	  public void start(boolean paramBoolean) {
	    if (this.dateline == null)
	      return; 
	    if (paramBoolean) {
	      this.dateline.start();
	    } else {
	      this.dateline.stop();
	    } 
	  }
	  
	  public void drain() {
	    if (this.dateline != null)
	      this.dateline.drain(); 
	  }
	  
	  public void close() {
	    if (this.dateline != null) {
	      this.dateline.stop();
	      this.dateline.close();
	    } 
	  }
	  
	  public void refreshMessage(String paramString) {
	    System.out.print(paramString);
	  }
	}